package com.fastjpa.service.impl;

import java.util.List;

import com.efeichong.multiTenant.MultiTenantIdHolder;
import com.fastjpa.vo.SaveStudentVo;
import org.springframework.stereotype.Service;
import com.fastjpa.dao.StudentDao;
import com.fastjpa.entity.Student;
import com.fastjpa.vo.StudentVo;
import com.fastjpa.service.StudentService;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;
import com.efeichong.common.PageData;
import com.efeichong.util.TransformUtils;
import com.efeichong.jpa.JExample;
import com.efeichong.exception.JpaException;

/**
* @author admin
* @date 2022/07/17
* @description 学生
*/
@RequiredArgsConstructor
@Service
public class StudentServiceImpl implements StudentService {

    private final StudentDao studentDao;

    /**
     * 通过id查询
     *
     * @param id 主键
     * @return
     */
    @Override
    public StudentVo getInfo(Long id){
        return studentDao.selectById(id, StudentVo.class).orElseThrow(() -> new JpaException("数据不存在！"));
    }

    /**
    * 分页查询
    *
    * @param studentVo
    * @return
    */
    @Override
    public PageData<StudentVo> selectByPage(StudentVo studentVo){
        MultiTenantIdHolder.setTenantId("gggggggg");
        JExample<Student> example = new JExample();
        example.initExample(studentVo);
        PageData<StudentVo> studentVoPageData = studentDao.selectByPage(example, StudentVo.class);
        MultiTenantIdHolder.remove();
        return studentVoPageData;
    }

    /**
     * 新增
     *
     * @param studentVo
     * @return
     */
    @Transactional
    @Override
    public void insertStudent(SaveStudentVo studentVo){
        studentDao.save(TransformUtils.toPo(studentVo,Student.class));
    }

    /**
     * 修改
     *
     * @param studentVo
     * @return
     */
    @Transactional
    @Override
    public void updateStudent(SaveStudentVo studentVo){
        if(studentVo.getId() == null){
            throw new JpaException("主键为必传参数");
        }
        studentDao.save(TransformUtils.toPo(studentVo,Student.class));
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids){
        studentDao.deleteAllByIds(ids);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public void deleteById(Long id){
        studentDao.deleteById(id);
    }
}
